<template>
  <view>
    <van-field
      v-model="valueText"
      v-show="!props.hidden"
      is-link
      :readonly="true"
      :label="props.formItem?.title"
      :placeholder="props.placeholder"
      :required="required"
      :rules="rules"
      :input-align="FieldInputAlign.DEFAULT"
      @click="showPicker = true"
    />
    <van-popup v-model:show="showPicker" round position="bottom">
      <van-picker title="请选择" :columns="options" @confirm="onConfirm" @cancel="onCancel" />
    </van-popup>
  </view>
</template>
<script setup lang="ts">
  import { isEmpty } from '@/utils/is';
  import { ref, computed, watch } from 'vue';
  import { evalFormual } from '@/utils/evalFormual';
  import { onLoad } from '@dcloudio/uni-app';
  import { FormOptionModel } from '@/services/model/formModel';
  import { FieldInputAlign } from '@/enums/formEnum';

  const emits = defineEmits(['update:value', 'labelShow']);

  const props = defineProps({
    value: {
      type: String,
      default: () => '',
    },
    required: {
      type: Boolean,
      default: false,
    },
    readonly: {
      type: Boolean,
      default: false,
    },
    //隐藏组件的公共属性
    formula: {
      type: Object,
      default: () => {
        return {
          showFormula: '', //回显用，因为包含表单字段的时候会出现表单名称
          calcFormula: '', //解析用，表单名称对应为formData.fieldxxxx
        };
      },
    },
    formItem: {
      type: Object,
      default: () => {},
    },
    formData: {
      type: Object,
      default: () => {
        return {};
      },
    },
    rule: {
      type: Object,
      default: () => null,
    },
    placeholder: {
      type: String,
      default: '请选择',
    },
    enableScan: {
      type: Boolean,
      default: false,
    },
    hidden: {
      type: Boolean,
      default: false,
    },
  });

  // field显示的值
  const valueText = ref<string>();

  // 是否显示下拉选
  const showPicker = ref<boolean>(false);

  // 下拉选数据
  const options = ref<FormOptionModel[]>([]);

  onLoad(() => {
    // 获取field中的下拉选数据
    let configOptions = props.formItem.props.options;
    for (let i = 0; i < configOptions.length; i++) {
      options.value.push({ text: configOptions[i], value: configOptions[i] });
    }
  });

  /** 计算属性：_value */
  const _value = computed({
    get() {
      return props.value;
    },
    set(val) {
      emits('update:value', val);
    },
  });

  /** 校验规则 */
  const rules = computed(() => {
    if (isEmpty(props.rule)) {
      return [];
    } else {
      return props.rule;
    }
  });

  const doFormula = () => {
    let hidden = evalFormual(props.formula.calcFormula, props.formData);
    //如果隐藏，同时也隐藏label,通知父组件去隐藏，因为label不在当前界面
    //调用父组件FormDesignRender 的方法：
    emits('labelShow', props.formItem, hidden);
  };

  /**
   * 确认选择
   * @param _value 选中的值
   */
  const onConfirm = (event: any) => {
    // 获取下拉选的value
    _value.value = event.selectedValues[0];
    // 获取下拉选的text
    valueText.value = event.selectedOptions[0].text;
    showPicker.value = false;
  };

  /**
   * 取消
   */
  const onCancel = () => {
    showPicker.value = false;
  };

  watch(
    () => props.formData,
    (newVal, oldVal) => {
      doFormula();
    },
    { immediate: true, deep: true },
  );
</script>
<style scoped lang="scss"></style>
